unit ec_changeinventorybase;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Buttons,
  StdCtrls, Spin, EditBtn, ec_base;

type

  { TECChangeInventoryBaseFrame }

  TECChangeInventoryBaseFrame = class(TECBaseFrame)
    ActionGroupBox: TGroupBox;
    AmountGroupBox: TGroupBox;
    IncludingEquippedCheckBox: TCheckBox;
    VariableEditButton: TEditButton;
    ItemComboBox: TComboBox;
    ItemLabel: TLabel;
    GiveRadioButton: TRadioButton;
    ConstantRadioButton: TRadioButton;
    VariableRadioButton: TRadioButton;
    ConstantSpinEdit: TSpinEdit;
    TakeRadioButton: TRadioButton;
    procedure AmountRadioButtonChange(Sender: TObject);
    procedure VariableEditButtonButtonClick(Sender: TObject);
    procedure VariableEditButtonKeyPress(Sender: TObject; var Key: char);
  private
    VariableId: Integer;
    procedure UpdateVariableName;
  public
    function GetCommandCode: Integer; virtual; abstract;
    function Equippable: Boolean; virtual; abstract;
    procedure InitStrings; virtual; abstract;
    procedure InitNew; override;
    procedure InitExisting; override;
    procedure MakeResultingCommand; override;

  end;

var
  ECChangeInventoryBaseFrame: TECChangeInventoryBaseFrame;

implementation

uses
  fpjson, namedindexselection;

{$R *.lfm}

{ TECChangeInventoryBaseFrame }

procedure TECChangeInventoryBaseFrame.AmountRadioButtonChange(Sender: TObject
  );
begin
  ConstantSpinEdit.Enabled := ConstantRadioButton.Checked;
  VariableEditButton.Enabled := VariableRadioButton.Checked;
  if VariableRadioButton.Checked then
    UpdateVariableName
end;

procedure TECChangeInventoryBaseFrame.VariableEditButtonButtonClick(
  Sender: TObject);
begin
  VariableId := NamedIndexSelectionForm.SelectVariable(VariableId, Db);
  UpdateVariableName
end;

procedure TECChangeInventoryBaseFrame.VariableEditButtonKeyPress(
  Sender: TObject; var Key: char);
begin
  Key := #0;
  VariableEditButtonButtonClick(Self)
end;

procedure TECChangeInventoryBaseFrame.UpdateVariableName;
begin
  VariableEditButton.Text := Db.GetVariableName(VariableId)
end;

procedure TECChangeInventoryBaseFrame.InitNew;
begin
  InitStrings;
  VariableId := 1;
  GiveRadioButton.Checked := True;
  ConstantRadioButton.Checked := True;
  AmountRadioButtonChange(Self);
end;

procedure TECChangeInventoryBaseFrame.InitExisting;
var
  Params: TJSONArray = nil;
  ItemId: Integer = 0;
  ActionCode: Integer = 0;
  AmountType: Integer = 0;
  AmountValue: Integer = 0;
  IncludeEqupped: Boolean = False;
begin
  InitStrings;
  VariableId := 1;

  Params := GetFirstParams;

  if (Params <> nil) and (Params.Count >= 1) and (Params[0].JSONType = jtNumber)
  then
    ItemId := Params[0].AsInteger;
  ItemComboBox.ItemIndex := ItemId - 1;

  if (Params <> nil) and (Params.Count >= 2) and (Params[1].JSONType = jtNumber)
  then
    ActionCode := Params[1].AsInteger;

  if ActionCode = 0 then
    GiveRadioButton.Checked := True
  else
    TakeRadioButton.Checked := True;

  if (Params <> nil) and (Params.Count >= 3) and (Params[2].JSONType = jtNumber)
  then
    AmountType := Params[2].AsInteger;

  if AmountType = 0 then
    ConstantRadioButton.Checked := True
  else
    VariableRadioButton.Checked := True;

  if (Params <> nil) and (Params.Count >= 4) and (Params[3].JSONType = jtNumber)
  then
    AmountValue := Params[3].AsInteger;

  if AmountType = 0 then
    ConstantSpinEdit.Value := AmountValue
  else begin
    VariableId := AmountValue;
    UpdateVariableName;
  end;

  if Equippable and (Params <> nil) and (Params.Count >= 5)
                       and (Params[4].JSONType = jtBoolean) then
    IncludeEqupped := Params[4].AsBoolean;

  if Equippable then
    IncludingEquippedCheckBox.Checked := IncludeEqupped;

  AmountRadioButtonChange(Self)
end;

procedure TECChangeInventoryBaseFrame.MakeResultingCommand;
  function ActionCode: Integer;
  begin
    if GiveRadioButton.Checked then
      ActionCode := 0
    else
      ActionCode := 1
  end;

  function AmountType: Integer;
  begin
    if ConstantRadioButton.Checked then
      AmountType := 0
    else
      AmountType := 1
  end;

  function AmountValue: Integer;
  begin
    if ConstantRadioButton.Checked then
      AmountValue := ConstantSpinEdit.Value
    else
      AmountValue := VariableId
  end;

var
  Params: TJSONArray;
begin
  Params := TJSONArray.Create([
    ItemComboBox.ItemIndex + 1,
    ActionCode,
    AmountType,
    AmountValue
  ]);

  if Equippable then
    Params.Add(IncludingEquippedCheckBox.Checked);

  ResultingCommand := SingleLineResultingCommand(GetCommandCode, Params);
end;

end.

